package com.base.cn.platform.os.controller.mobile.user.sign;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.user.signin.CusUserSignInService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * H5用户签到Controller
 *
 * @author zsw
 * @create 2018/6/27
 */
@Controller
@RequestMapping("/mobile/uc/sign")
public class MobileUserSignController extends MobileBaseController {

    @Autowired
    private CusUserService userService;
    @Autowired
    private CusUserSignInService userSignInService;

    /**
     * 签到数据
     *
     * @param year  年 默认当前年
     * @param month 月 默认当前月
     */
    @RequestMapping("/signData")
    @ResponseBody
    public Map<String, Object> getSignData(HttpServletRequest request,
                                           @RequestParam(value = "year", required = false) Integer year,
                                           @RequestParam(value = "month", required = false) Integer month) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        List<Map<String, Object>> result = userSignInService.findUserMonthSignList(userId,String.valueOf(year),String.valueOf(month));
        Map<String,Object> dataMap = new HashMap<>();
        Map<Object,Object> objMap = null;
        int monthSign = 0;
        if(ObjectUtils.isNotEmpty(result)){
            result.forEach(e->{
                String signDate  = e.get("signDate").toString();
                signDate = DateUtils.format(DateUtils.parseDate(signDate,"yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd");
                e.put("signDate",signDate);
            });
            monthSign = result.size();
            objMap = result.stream().collect(Collectors.toMap(e->e.get("signDate"),e->e));

        }
        dataMap.put("monthSign",monthSign);
        dataMap.put("objMap",objMap);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 签到
     */
    @RequestMapping("/sign")
    @ResponseBody
    public Map<String, Object> saveSignData(HttpServletRequest request) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        HashMap<String, Object> sign = new HashMap<>();
        sign.put("userId",userId);
        sign.put("signDate",new Date());
        return userSignInService.saveUserSignIn(sign);
    }

    /**
     * 签到首页
     */
    @RequestMapping("/signIndex")
    public String toSignPage(HttpServletRequest request,
                             Model model) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> userInfo = userService.userInfo(userId, false, false, false, false, false, false, false);
        Object signNum = userInfo.get("signNum");
        boolean isSign = userSignInService.checkUserIsSign(userId, DateUtils.format(new Date(), "yyyy-MM-dd"));
        model.addAttribute("isSign",isSign);
        model.addAttribute("signNum",signNum);
        return resultPage("/user/sign/sign");
    }

}
